package _99练习;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SellerRunnable3 implements Runnable {

	int count = 100;
	int s1, s2;
	Lock l = new ReentrantLock();

	@Override
	public void run() {
		while (count > 0) {

			// System.out.println(name);
			int time = 100;
			if (l.tryLock()) {
				count--;
				System.out.println(Thread.currentThread().getName() + "吃了1个包子，剩下" + count + "个包子");
				if (Thread.currentThread().getName().equals("小明")) {
					s2++;
					time = (int) (Math.random() * 60 + 20);
				} else {
					s1++;
					time = (int) (Math.random() * 40 + 10);
				}
				// 解锁
				l.unlock();
			} else {

			}

			try {
				Thread.sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName() + "吃完啦");
		System.out.println("小明吃了" + s2 + "只，大明吃了" + s1 + "只");
	}

}
